package cn.yh.register.order.service.impl;

import cn.yh.register.enums.RefundStatusEnum;
import cn.yh.register.model.order.PaymentInfo;
import cn.yh.register.model.order.RefundInfo;
import cn.yh.register.order.mapper.RefundInfoMapper;
import cn.yh.register.order.service.RefundInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {


    /**
     * 保存退款记录
     * @param paymentInfo 交易记录
     * @return 保存的退款记录
     */
    @Override
    public RefundInfo saveRegundInfo(PaymentInfo paymentInfo) {
        //先查询退款记录
        RefundInfo refundInfo = baseMapper.selectOne(new LambdaQueryWrapper<RefundInfo>()
                .eq(RefundInfo::getOrderId, paymentInfo.getOrderId())
                .eq(RefundInfo::getPaymentType, paymentInfo.getPaymentType()));
        if (refundInfo != null) {
            return refundInfo;
        }
        refundInfo = new RefundInfo() ;
        refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
        refundInfo.setOrderId(paymentInfo.getOrderId());
        refundInfo.setPaymentType(paymentInfo.getPaymentType());
        refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
        refundInfo.setSubject(paymentInfo.getSubject());
        refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());  // 1：退款中
        baseMapper.insert(refundInfo);
        return refundInfo;
    }

}
